package com.agileai.pm.module.base.handler;

import java.util.Date;
import java.util.List;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardListHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.module.base.service.ProjectTestManage;
import com.agileai.util.StringUtil;

public class ProjectTestManageListHandler
        extends StandardListHandler {
    public ProjectTestManageListHandler() {
        super();
        this.editHandlerClazz = ProjectTestManageEditHandler.class;
        this.serviceId = buildServiceId(ProjectTestManage.class);
    }
    
    public ViewRenderer prepareDisplay(DataParam param){
		mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		List<DataRow> rsList = getService().findRecords(param);
		DataRow initializeState = getService().getSubmitStateRecords(param.get("PI_ID"));
		DataRow submitState = getService().getSubmitState(param.get("PI_ID"));
		if(initializeState != null){
			param.put("initializeState", initializeState.stringValue("PT_STATE"));
		}else if(submitState != null){
			param.put("initializeState", submitState.stringValue("PT_STATE"));
		}else if(initializeState == null && submitState == null){
			param.put("initializeState", "empty");
		}
		this.setAttributes(param);
		this.setRsList(rsList);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}

    protected void processPageAttributes(DataParam param) {
        setAttribute("PT_STATE",FormSelectFactory.create("TS_STATE").addSelectedValue(param.get("PT_STATE")));
        initMappingItem("PT_STATE",FormSelectFactory.create("TS_STATE").getContent());
    }
    
    @PageAction
    public ViewRenderer submitTestScheme(DataParam param){
    	String rspText = "";
    	String testSchemeId = param.get("PT_ID");
    	User user = (User) this.getUser();
    	String userId = user.getUserId();
    	Date releaseTime = new Date();
    	DataRow submitState = getService().getSubmitState(param.get("PI_ID"));
    	if(submitState == null){
    		getService().updateSubmitTestSchemeRecord(testSchemeId,userId,releaseTime);
    		rspText = "0";
    	}else{
    		rspText = "1";
    	}
		return new AjaxRenderer(rspText);
	}
    
    @PageAction
    public ViewRenderer fileTestScheme(DataParam param){
    	String testSchemeId = param.get("PT_ID");
    	getService().updateFileTestSchemeRecord(testSchemeId);
		return prepareDisplay(param);
	}
    
    @PageAction
    public ViewRenderer extractDefectRecord(DataParam param){
    	String rspText = "";
    	List<DataRow> fileTestScheme = getService().findFileTestSchemeRecord(param);
    	String testSchemeId = fileTestScheme.get(0).getString("PT_ID");
    	int defectRecord = 0;
    	List<DataRow> notCompleteDefectRecord = getService().findNotCompleteDefectRecord(testSchemeId);
    	for(int i = 0; i<notCompleteDefectRecord.size();i++){
    		DataRow row = notCompleteDefectRecord.get(i);
    		String key = KeyGenerator.instance().genKey();
    		param.put("TPD_ID",key);
    		param.put("PT_ID", param.stringValue("PT_ID"));
    		param.put("PF_ID", row.stringValue("PF_ID"));
    		param.put("TPD_NAME", row.stringValue("TPD_NAME"));
    		
    		String TPD_PROCESS_TIME = row.stringValue("TPD_PROCESS_TIME");
    		if(StringUtil.isNotNullNotEmpty(TPD_PROCESS_TIME)){
    			param.put("TPD_PROCESS_TIME", TPD_PROCESS_TIME);
    		}
    		param.put("TPD_PROPERSON_ID", row.stringValue("TPD_PROPERSON_ID"));
    		param.put("TPD_TESTPERSON_ID", row.stringValue("TPD_TESTPERSON_ID"));
    		param.put("TPD_RELEASE_TIME", row.stringValue("TPD_RELEASE_TIME"));
    		param.put("TPD_DESC", row.stringValue("TPD_DESC"));
    		param.put("TPD_STATE", row.stringValue("TPD_STATE"));
    		param.put("TPD_FUNCTION", row.stringValue("TPD_FUNCTION"));
    		param.put("TPD_TYPE", row.stringValue("TPD_TYPE"));
    		param.put("TPD_PRIORITY", row.stringValue("TPD_PRIORITY"));
    		getService().insertNotCompleteDefectRecord(param);
    		getService().updateExtractDefectRecord(key);//提过来的状态全改为初始化
    		defectRecord ++;
    	}
    	rspText = String.valueOf(defectRecord);
		return new AjaxRenderer(rspText);
	}

    protected void initParameters(DataParam param) {
        initParamItem(param, "PT_NAME", "");
        initParamItem(param, "PT_STATE", "");
    }

    protected ProjectTestManage getService() {
        return (ProjectTestManage) this.lookupService(this.getServiceId());
    }
}
